package terminal

import (
	"context"
	"time"

	"device-admin/config/redis"
	"device-admin/internal/define"
)

// SessionLock 会话加锁
func SessionLock(robotID, terminalType string) bool {
	rdb := redis.NewDB()
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
	defer cancel()
	success, _ := rdb.SetNX(ctx, define.RedisDeviceSessionLock+robotID+":"+terminalType, time.Now().Unix(), time.Second*120).Result()
	return success
}

// SessionUnLock 会话释放锁
func SessionUnLock(robotID, terminalType string) {
	rdb := redis.NewDB()
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
	defer cancel()
	rdb.Del(ctx, define.RedisDeviceSessionLock+robotID+":"+terminalType)
}

// ExpireSessionLock 会话锁延期
func ExpireSessionLock(robotID, terminalType string) {
	rdb := redis.NewDB()
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
	defer cancel()
	rdb.Expire(ctx, define.RedisDeviceSessionLock+robotID+":"+terminalType, time.Second*120)
}
